package com.controller;

import java.io.File;
import java.io.IOException;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.entity.Tongzhigonggao;
import com.server.TongzhigonggaoServer;
import com.util.PageBean;
import net.sf.json.JSONObject;
import com.util.db;
import java.sql.SQLException;
import java.sql.*;
@Controller
public class TongzhigonggaoController {
	@Resource
	private TongzhigonggaoServer tongzhigonggaoService;

	@RequestMapping("addTongzhigonggao.do")
	public String addTongzhigonggao(HttpServletRequest request,Tongzhigonggao tongzhigonggao,HttpSession session) throws SQLException{
		Timestamp time=new Timestamp(System.currentTimeMillis());
		
		tongzhigonggao.setAddtime(time.toString().substring(0, 19));
		tongzhigonggaoService.add(tongzhigonggao);
		db dbo = new db();
		
		//kuabiaogaizhi
		session.setAttribute("backxx", "添加成功");session.setAttribute("backurl", request.getHeader("Referer"));return "redirect:postback.jsp";
		
	}
	@RequestMapping("addTongzhigonggaoqt.do")
	public String addTongzhigonggaoqt(HttpServletRequest request,Tongzhigonggao tongzhigonggao,HttpSession session) throws SQLException{
		Timestamp time=new Timestamp(System.currentTimeMillis());
		
		tongzhigonggao.setAddtime(time.toString().substring(0, 19));
		tongzhigonggaoService.add(tongzhigonggao);
		db dbo = new db();
		
		//kuabiaogaizhi
		session.setAttribute("backxx", "添加成功");
		session.setAttribute("backurl", request.getHeader("Referer"));
		return "redirect:postback.jsp";
	}
 
//	处理编辑
	@RequestMapping("doUpdateTongzhigonggao.do")
	public String doUpdateTongzhigonggao(int id,ModelMap map,Tongzhigonggao tongzhigonggao){
		tongzhigonggao=tongzhigonggaoService.getById(id);
		map.put("tongzhigonggao", tongzhigonggao);
		return "tongzhigonggao_updt";
	}
	
	
	
	
//	后台详细
	@RequestMapping("tongzhigonggaoDetail.do")
	public String tongzhigonggaoDetail(int id,ModelMap map,Tongzhigonggao tongzhigonggao){
		tongzhigonggao=tongzhigonggaoService.getById(id);
		map.put("tongzhigonggao", tongzhigonggao);
		return "tongzhigonggao_detail";
	}
//	前台详细
	@RequestMapping("tzggDetail.do")
	public String tzggDetail(int id,ModelMap map,Tongzhigonggao tongzhigonggao){
		tongzhigonggao=tongzhigonggaoService.getById(id);
		map.put("tongzhigonggao", tongzhigonggao);
		return "tongzhigonggaodetail";
	}
//	
	@RequestMapping("updateTongzhigonggao.do")
	public String updateTongzhigonggao(int id,ModelMap map,Tongzhigonggao tongzhigonggao,HttpServletRequest request,HttpSession session){
		tongzhigonggaoService.update(tongzhigonggao);
		
		session.setAttribute("backxx", "修改成功");session.setAttribute("backurl", request.getHeader("Referer"));return "redirect:postback.jsp";
	}

//	分页查询
	@RequestMapping("tongzhigonggaoList.do")
	public String tongzhigonggaoList(@RequestParam(value="page",required=false)String page,
			ModelMap map,HttpSession session,Tongzhigonggao tongzhigonggao, String biaoti, String neirong){
		if(page==null||page.equals("")){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page), 8);
		Map<String, Object> pmap=new HashMap<String,Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", 8);
		
		
		if(biaoti==null||biaoti.equals("")){pmap.put("biaoti", null);}else{pmap.put("biaoti", biaoti);}
		if(neirong==null||neirong.equals("")){pmap.put("neirong", null);}else{pmap.put("neirong", neirong);}
		
		int total=tongzhigonggaoService.getCount(pmap);
		pageBean.setTotal(total);
		List<Tongzhigonggao> list=tongzhigonggaoService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 1);
		return "tongzhigonggao_list";
	}
	@RequestMapping("tongzhigonggaoList2.do")
	public String tongzhigonggaoList2(@RequestParam(value="page",required=false)String page,
			ModelMap map,HttpSession session,Tongzhigonggao tongzhigonggao, String biaoti, String neirong){
		if(page==null||page.equals("")){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page), 8);
		Map<String, Object> pmap=new HashMap<String,Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", 8);
		
		
		if(biaoti==null||biaoti.equals("")){pmap.put("biaoti", null);}else{pmap.put("biaoti", biaoti);}
		if(neirong==null||neirong.equals("")){pmap.put("neirong", null);}else{pmap.put("neirong", neirong);}
		
		int total=tongzhigonggaoService.getCount(pmap);
		pageBean.setTotal(total);
		List<Tongzhigonggao> list=tongzhigonggaoService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 1);
		return "tongzhigonggao_list2";
	}
	
	@RequestMapping("tzggList.do")
	public String tzggList(@RequestParam(value="page",required=false)String page,
			ModelMap map,HttpSession session,Tongzhigonggao tongzhigonggao, String biaoti, String neirong){
		if(page==null||page.equals("")){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page), 8);
		Map<String, Object> pmap=new HashMap<String,Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", 8);
		if(biaoti==null||biaoti.equals("")){pmap.put("biaoti", null);}else{pmap.put("biaoti", biaoti);}
		if(neirong==null||neirong.equals("")){pmap.put("neirong", null);}else{pmap.put("neirong", neirong);}
		
		int total=tongzhigonggaoService.getCount(pmap);
		pageBean.setTotal(total);
		List<Tongzhigonggao> list=tongzhigonggaoService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 1);
		return "tongzhigonggaolist";
	}
	@RequestMapping("tzggListtp.do")
	public String tzggListtp(@RequestParam(value="page",required=false)String page,
			ModelMap map,HttpSession session,Tongzhigonggao tongzhigonggao, String biaoti, String neirong){
		if(page==null||page.equals("")){
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page), 8);
		Map<String, Object> pmap=new HashMap<String,Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", 8);
		if(biaoti==null||biaoti.equals("")){pmap.put("biaoti", null);}else{pmap.put("biaoti", biaoti);}
		if(neirong==null||neirong.equals("")){pmap.put("neirong", null);}else{pmap.put("neirong", neirong);}
		
		int total=tongzhigonggaoService.getCount(pmap);
		pageBean.setTotal(total);
		List<Tongzhigonggao> list=tongzhigonggaoService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 1);
		return "tongzhigonggaolisttp";
	}
	
	@RequestMapping("deleteTongzhigonggao.do")
	public String deleteTongzhigonggao(int id,HttpServletRequest request,HttpSession session){
		tongzhigonggaoService.delete(id);
		session.setAttribute("backxx", "删除成功");session.setAttribute("backurl", request.getHeader("Referer"));return "redirect:postback.jsp";
	}
	
	
}
